<?php
class ControllerJournal2Snippets extends Controller {

    private $s_title = null;
    private $s_description = null;
    private $s_url = null;
    private $s_image = null;
    private $s_rating = null;

    public function index() {
        $this->load->model('tool/image');

        switch ($this->journal2->page->getType()) {
            case 'product':
                $this->load->model('catalog/product');
                $product_info = $this->model_catalog_product->getProduct($this->journal2->page->getId());
                if ($product_info) {
                    $this->s_title = $product_info['name'];
                    $this->s_description = trim(utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, 150));
                    $url = '';
                    if (isset($this->request->get['path'])) {
                        $url .= '&path=' . $this->request->get['path'];
                    }
                    $url .= '&product_id=' . $product_info['product_id'];
                    $this->s_url = $this->url->link('product/product', $url);
                    $this->s_image = $this->model_tool_image->resize($product_info['image'], 200, 200);
                    $this->s_rating = (int)$product_info['rating'];

                    $this->journal2->settings->set('product_google_snippet', 'itemscope itemtype="http://schema.org/Product"');
                    $this->journal2->settings->set('product_num_reviews', $product_info['reviews']);
                    $this->journal2->settings->set('product_in_stock', $product_info['quantity'] > 0 ? 'yes' : 'no');
                }
                break;

            case 'category':
                $this->load->model('catalog/category');
                $parts = explode('_', $this->journal2->page->getId());
                $category_id = (int)array_pop($parts);
                $category_info = $this->model_catalog_category->getCategory($category_id);
                if ($category_info) {
                    $this->s_title = $category_info['name'];
                    $this->s_description = trim(utf8_substr(strip_tags(html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8')), 0, 150));
                    $this->s_url = $this->url->link('product/category', '&path=' . $this->journal2->page->getId());
                    $this->s_image = $this->model_tool_image->resize($category_info['image'], 200, 200);
                }
                break;
            default:
                $this->s_title = $this->config->get('config_name');
                $this->s_description = $this->config->get('config_meta_description') . '...';
                $this->s_url = $this->config->get('config_url');
                $this->s_image = $this->model_tool_image->resize($this->config->get('config_logo'), 200, 200);
                break;
        }

        $this->journal2->settings->set('fb_meta', array(
            'og:title'          => $this->s_title,
            'og:description'    => $this->s_description,
            'og:url'            => $this->s_url,
            'og:image'          => $this->s_image
        ));
    }

}
?>